package org.example.school.controller;

import org.example.school.business.StudentInfo;
import org.example.school.common.Result;
import org.example.school.entity.EventRegistrations;
import org.example.school.entity.SchoolEvents;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 学生控制器
 * 处理学生相关操作，如活动报名等
 */
@RestController
@RequestMapping("/student")
@CrossOrigin
public class StudentController {

    @Autowired
    private StudentInfo studentInfo;

    /**
     * 获取所有活动
     * @return 活动列表
     */
    @GetMapping("/events")
    public Result<List<SchoolEvents>> getEvents() {
        try {
            List<SchoolEvents> events = studentInfo.getAllEvents();
            return Result.success(events);
        } catch (Exception e) {
            return Result.error("获取活动列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取我的报名
     * @return 报名列表
     */
    @GetMapping("/my-registrations")
    public Result<List<EventRegistrations>> getMyRegistrations() {
        try {
            List<EventRegistrations> registrations = studentInfo.getMyRegistrations();
            return Result.success(registrations);
        } catch (Exception e) {
            return Result.error("获取报名列表失败: " + e.getMessage());
        }
    }

    /**
     * 报名活动
     * @param request 报名请求
     * @return 报名结果
     */
    @PostMapping("/events/register")
    public Result<String> registerEvent(@RequestBody RegistrationRequest request) {
        try {
            String result = studentInfo.registerEvent(request.getEventId());
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("报名失败: " + e.getMessage());
        }
    }

    /**
     * 取消报名
     * @param request 取消报名请求
     * @return 取消结果
     */
    @PostMapping("/events/cancel-registration")
    public Result<String> cancelRegistration(@RequestBody CancelRegistrationRequest request) {
        try {
            String result = studentInfo.cancelRegistration(request.getRegistrationId());
            return Result.success(result);
        } catch (Exception e) {
            return Result.error("取消报名失败: " + e.getMessage());
        }
    }
    
    /**
     * 报名请求DTO
     */
    public static class RegistrationRequest {
        private Long eventId;

        public Long getEventId() {
            return eventId;
        }

        public void setEventId(Long eventId) {
            this.eventId = eventId;
        }
    }
    
    /**
     * 取消报名请求DTO
     */
    public static class CancelRegistrationRequest {
        private Long registrationId;

        public Long getRegistrationId() {
            return registrationId;
        }

        public void setRegistrationId(Long registrationId) {
            this.registrationId = registrationId;
        }
    }
}